<template>
  <div class="outer-container">
    <div class="space">
      <!-- <div class="space-head"></div> -->
      <div class="space-mis">
        <el-card style="width: 80%; margin: 10px auto; text-align: center;">
          <el-form ref="formRef" :rules="data.rules" :model="data.form" label-width="100px">
            <el-form-item label="原密码" prop="password">
              <el-input v-model="data.form.password" show-password placeholder="请输入原密码"/>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="data.form.newPassword" show-password placeholder="请输入新密码"/>
            </el-form-item>
            <el-form-item label="确认新密码" prop="confirmPassword">
              <el-input v-model="data.form.confirmPassword" show-password placeholder="请输入确认新密码"/>
            </el-form-item>
          </el-form>
          <div style="text-align: center">
            <el-button type="primary" @click="update">修改密码</el-button>
            <el-button type="primary" @click="back">返回</el-button>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from "vue";
import request from "@/utils/request";
import {ElMessage} from "element-plus";

const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请再次输入新密码'))
  } else if (value !== data.form.newPassword) {
    callback(new Error('两次输入新密码不一致!'))
  } else {
    callback()
  }
}

const formRef = ref()

const data = reactive({
  user: JSON.parse(localStorage.getItem("xm-pro-user")),
  form: {},
  rules: {
    password: [
      {required: true, message: '请输入密码', trigger: 'blur'}
    ],
    newPassword: [
      {required: true, message: '请输入新密码', trigger: 'blur'}
    ],
    confirmPassword: [
      {validator: validatePass, trigger: 'blur'}
    ]
  }
})
const update = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      checkPassword()
    } else {
      ElMessage.error("请检查输入")
      return false
    }
  })
}

const checkPassword = () => {
  request.get("/user/getPassword", {
    params: {
      id: data.user.id,
      password: data.form.password
    }
  }).then(res => {
    if (res.data === 0) {
      data.user.password = data.form.newPassword
      request.put("/user/updatePassword", data.user).then(res => {
        if (res.code === '0') {
          ElMessage.success("修改成功")
          localStorage.setItem("xm-pro-user", JSON.stringify(data.user))
          setTimeout(() => {
            location.href = "/myspace"
          }, 500)
        } else {
          ElMessage.error(res.msg)
        }
      })
    } else {
      ElMessage.error("原密码错误")
    }
  })
}

const back = () => {
  location.href = "/myspace"
}
</script>

<style scoped>
.outer-container {
  background-image: url("https://img2.baidu.com/it/u=2783925180,1561102608&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1422");
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh; /* 使容器占满整个视口高度，可以根据需要调整 */
//border: 2px solid #0e0c0c; /* 大边框 */
  padding: 20px; /* 内边距，可以根据需要调整 */
  box-sizing: border-box; /* 确保内边距不影响布局宽度 */
}

.space {
  width: 100%;
  max-width: 600px; /* 限制最大宽度，以便在大屏幕上居中 */
  text-align: center;
}

.space-head {
  width: 40%;
  margin: 10px auto; /* 水平居中 */
//border: #0e0c0c 1px solid;
}

.space-mis {
  margin: 0 auto; /* 水平居中 */
}
</style>